﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class UpdateLawDocumentItem : WebPart
    {
      
        public override string WebPartId
        {
            get
            {
                return "UpdateLawDocumentItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Sửa thông số văn bản tài liệu");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateLawDocumentItem));
            Environments.CheckSessionTimeOut(true);
            string LawDocumentItemId = (string)Environments.Request("LawDocumentItemId");
            LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);

            PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
            Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemCls.LawDocumentItemId, LawDocumentItemCls.Owner, true);

            string AllSubject = ResourceBLL.GetText(WebPartId, "SelectSubject", "== Chọn từ danh sách ==");
            DataTable dtLawDocumentOffice = new LawDocumentOfficeBLL().ReadingLawDocumentOffice("");
            string SelectLawDocumentOffice = "<select id=drpSelectLawDocumentOffice>";
            for (int iIndex = 0; iIndex < dtLawDocumentOffice.Rows.Count; iIndex++)
            {
                string LawDocumentOfficeId = (string)dtLawDocumentOffice.Rows[iIndex]["LawDocumentOfficeId"];
                string Subject = (string)dtLawDocumentOffice.Rows[iIndex]["Subject"];
                if (LawDocumentOfficeId.Equals(LawDocumentItemCls.LawDocumentOfficeId))
                {
                    SelectLawDocumentOffice += "<option selected value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
                }
                else
                {
                    SelectLawDocumentOffice += "<option value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
                }
            }
            SelectLawDocumentOffice += "</select>";



            DataTable dtLawDocumentType = new LawDocumentTypeBLL().ReadingLawDocumentType("");
            string SelectLawDocumentType = "<select id=drpSelectLawDocumentType>";
            for (int iIndex = 0; iIndex < dtLawDocumentType.Rows.Count; iIndex++)
            {
                string LawDocumentTypeId = (string)dtLawDocumentType.Rows[iIndex]["LawDocumentTypeId"];
                string Subject = (string)dtLawDocumentType.Rows[iIndex]["Subject"];
                if (LawDocumentItemCls.LawDocumentTypeId.Equals(LawDocumentTypeId))
                {
                    SelectLawDocumentType += "<option selected value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
                }
                else
                {
                    SelectLawDocumentType += "<option value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
                }
            }
            SelectLawDocumentType += "</select>";

            DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner("");
            string SelectLawDocumentSigner = "<select id=drpSelectLawDocumentSigner>";
            for (int iIndex = 0; iIndex < dtLawDocumentSigner.Rows.Count; iIndex++)
            {
                string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                string Subject = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                if (LawDocumentItemCls.LawDocumentSignerId.Equals(LawDocumentSignerId))
                {
                    SelectLawDocumentSigner += "<option selected value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
                }
                else
                {
                    SelectLawDocumentSigner += "<option value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
                }
            }
            SelectLawDocumentSigner += "</select>";

            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "LawDocument.js") +
                "<table>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Code", "Số") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtCode value=\""+LawDocumentItemCls.Code+"\"></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtSubject value=\"" + LawDocumentItemCls.Subject+ "\"></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtKeyword value=\"" + LawDocumentItemCls.Keyword+ "\"></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + ":</td>" +
                "           <td><textarea class=cssBodyLawDocument id=txtBody>" + LawDocumentItemCls.Body + "</textarea></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Signer", "Người ký") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterSigner><input type=button value=\"...\" onclick=detectUpdateSigner();><span id=divSigner>" + SelectLawDocumentSigner + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Office", "Đơn vị phát hành") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterOffice><input type=button value=\"...\" onclick=detectUpdateOffice();><span id=divOffice>" + SelectLawDocumentOffice + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "DocType", "Loại văn bản") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterDoctype><input type=button value=\"...\" onclick=detectUpdateDocType();><span id=divDocumentType>" + SelectLawDocumentType + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "ReleaseDate", "Ngày ban hành") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtReleasedDate", LawDocumentItemCls.ReleasedDate)) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "EffectedDate", "Ngày hiệu lực") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtEffectedDate", LawDocumentItemCls.EffectedDate)) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "ExpiredDate", "Ngày hết hạn") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtExpiredDate", LawDocumentItemCls.ExpiredDate)) + "</td>" +
                "       </tr>" +
                 "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "EntryDate", "Ngày nhập") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.GenerateLookUpDate("txtEntryDate", LawDocumentItemCls.EntryDate) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td></td>" +
                "           <td><input onclick=javascript:SaveUpdateLawDocumentItem('"+LawDocumentItemId+"'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "saveButton", "Ghi lại") + "\"><input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\"></td>" +
                "       </tr>" +
                "</table>";
            RetHtml = Environments.GetFormDialog(RetHtml, Title);
            return RetHtml;
        }


        
        [AjaxPro.AjaxMethod]
        public string Save(
            string LawDocumentItemId,
            string Code,
            string Subject,
            string Keyword,
            string Body,
            string LawDocumentTypeId,
            string LawDocumentOfficeId,
            string LawDocumentSignerId,
            string ReleasedDate,
            string EffectedDate,
            string ExpiredDate,
            string EntryDate)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);

                if (Code.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "CodeIsMissing", "Chưa nhập số văn bản!"));
                if (Subject.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "SubjectIsMissing", "Chưa tiêu đề văn bản!"));
                if (LawDocumentTypeId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentTypeIsMissing", "Chưa chọn loại văn bản!"));
                if (LawDocumentSignerId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentSignerIsMissing", "Chưa chọn người ký văn bản!"));
                if (LawDocumentOfficeId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentOfficeIsMissing", "Chưa chọn đơn vị phát hành!"));


                new LawDocumentBLL().SaveLawDocumentItem(null, LawDocumentItemId, Code, Subject, Keyword, Body, LawDocumentTypeId, LawDocumentOfficeId, LawDocumentSignerId, ReleasedDate, EffectedDate, ExpiredDate, EntryDate);
                return ResourceBLL.GetText(WebPartId,"SaveItemOk","Cập nhật văn bản pháp quy thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerDetectSigner(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner(Keyword);
                string SelectLawDocumentSigner = "<select id=drpSelectLawDocumentSigner>";
                //SelectLawDocumentSigner += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentSigner.Rows.Count; iIndex++)
                {
                    string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                    string Subject = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                    SelectLawDocumentSigner += "<option value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentSigner += "</select>";
                return SelectLawDocumentSigner;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerDetectOffice(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentOffice = new LawDocumentOfficeBLL().ReadingLawDocumentOffice(Keyword);
                string SelectLawDocumentOffice = "<select id=drpSelectLawDocumentOffice>";
                //SelectLawDocumentOffice += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentOffice.Rows.Count; iIndex++)
                {
                    string LawDocumentOfficeId = (string)dtLawDocumentOffice.Rows[iIndex]["LawDocumentOfficeId"];
                    string Subject = (string)dtLawDocumentOffice.Rows[iIndex]["Subject"];
                    SelectLawDocumentOffice += "<option value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentOffice += "</select>";
                return SelectLawDocumentOffice;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerDetectDocumentType(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentType = new LawDocumentTypeBLL().ReadingLawDocumentType(Keyword);
                string SelectLawDocumentType = "<select id=drpSelectLawDocumentType>";
                //SelectLawDocumentType += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentType.Rows.Count; iIndex++)
                {
                    string LawDocumentTypeId = (string)dtLawDocumentType.Rows[iIndex]["LawDocumentTypeId"];
                    string Subject = (string)dtLawDocumentType.Rows[iIndex]["Subject"];
                    SelectLawDocumentType += "<option value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentType += "</select>";
                return SelectLawDocumentType;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
        }
    }
}